/** @mainpage Software Regression Testing
* @author Elie M. Shaccour (elie.shaccour@gmail.com)
* @version Fall 2012-13
* @par Description
*	- This software allows programmers to keep track of def-use pairs across regression releases of a software
*	- Test criteria are in the form of du<x,y> pairs
*	- Once a new version of the tool is to be released it is introduced in the 'same directory' as the previous version and the tool is run to 
*generate test requirement
*	- The test requirements are in the form of an ordered assembly line address precedence
*/

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <fstream>

#include "main.h"
#include "util.h"
#include "addtest.h"
#include "tests.h"

using namespace std;

extern string ProjectDirectoryPathGlobalVariable;
AddTest *Work;


int main()
{
cout<<"************************************"<<endl;
cout<<"**Software Regression Testing v1.0**"<<endl;
cout<<"************************************"<<endl;
MainInteractive();
return 0;
}

void MainInteractive()
{
bool ExitCondition=false, IsProjectInfoDataAvailable=false;
string UserInput;

while(IsProjectInfoDataAvailable==false)
	{
	cout<<"Please type in the full path of the directory in which the software you would like to test is found:";
	cin>>UserInput;
	IsProjectInfoDataAvailable=IsProjPathValid(UserInput);
	if(IsProjectInfoDataAvailable==false)
		{
		cout<<"Invalid path. Please make sure the absolute directory where your project is found is entered and that the project is in that directory\n";
		}
	}

ProjectDirectoryPathGlobalVariable=UserInput;
	
while(ExitCondition==false)
	{
	cout<<"Menu Options:"<<endl;
	cout<<"Press 'work' to add test criterion to a project"<<endl;
	cout<<"Press 'test' to generate test rules for a project"<<endl;
	cout<<"Press 'logout' to quit"<<endl;

	cin>>UserInput;

	transform(UserInput.begin(),UserInput.end(),UserInput.begin(),::tolower);
	if(UserInput=="logout")///User wants to logout of the system
		{
		ExitCondition=true;
		}
	else if(UserInput=="work") ///user wants to work. ie add TC to the test suite
		{
		UserWork();
		}
	else if(UserInput=="test") ///user wants to generate a test rule for a project
		{
		UserGenerateTest();
		}
	else
		{
		//TODO:setup error reporting of unavailable command
		}
	}
return;
}

void UserWork()
{
///Setup object needed to work
Work=new AddTest(false);
string UserInput="test";
getline(cin,UserInput);//Had to put it so that getline in the loop work, no idea why

///While work is valid loop with data to goforward/backward until the user logout of the interface
while(Work->IsValid())
	{
	cout<<Work->GetPrint();
	cout<<"Enter your choice (type back to return to the previous menu):";
	getline(cin,UserInput);
	
	///"back" means go backward
	if(UserInput.compare("back")==0)
		{
		Work->GoBackward();
		}
	else if(UserInput.compare("logout")==0)///Quit the current interface
		{
		return;
		}
	else
		Work->GoForward(UserInput);///Go forward with the decided choice
	}

}


void UserGenerateTest()
{
Tests Test(false,Work->GetTestCriteria(),Work->GetTestFile(),ProjectDirectoryPathGlobalVariable);
string UserInput="test";
getline(cin,UserInput);//Had to put it so that getline in the loop work, no idea why

///While valid
while(Test.IsValid())
        {
        cout<<Test.GetPrint();
        cout<<"Enter your choice (type back to return to the previous menu):";
        getline(cin,UserInput);
        if(UserInput.compare("logout")==0)///Quit the current interface
                {
                return;
                }
        else
                Test.GoForward(UserInput);///Go forward with the decided choice
        }
}

bool IsProjPathValid(std::string ProjDirectoryPath)
{
ifstream IsPathAvailable(ProjDirectoryPath.c_str());///Path available
if(IsPathAvailable)
	{
	        return true;
	}
else	
	{
	return false;
	}
}

